<template>
    <div class="user-add-container">
        <el-dialog title="编辑博客类型" :visible.sync="dialogVisible" width="400px" @close="handleCancel()">
            <el-form :model="userForm" :rules="userRules" ref="userFormRef" label-width="120px">
                <el-form-item label="博客类型名称" prop="username">
                    <el-input v-model="userForm.name" placeholder="请输入博客类型名称"></el-input>
                </el-form-item>
                <el-form-item label="博客类型值" prop="password">
                    <el-input v-model="userForm.value" placeholder="请输入博客类型值"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleSubmit">确定</el-button>
                <el-button @click="handleCancel">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { blogTypesEdit } from '@/api/api'
export default {
    name: 'blogTypesEdit',
    data () {
        return {
            dialogVisible: false,
            userForm: {
                _id: '',
                name: '',
                value: ''
            },
            userRules: {
                name: [{ required: true, message: '请输入博客类型名称', trigger: 'blur' }],
                value: [{ required: true, message: '请输入博客类型值', trigger: 'blur' }]
            }
        }
    },
    mounted () {
        
    },
    methods: {
        openDialog (item) {
            console.log("测试~~~",item)
            this.userForm = {
                _id: item._id,
                name: item.name,
                value: item.value
            }
            this.dialogVisible = true
        },
        handleSubmit () {
            this.$refs.userFormRef.validate(valid => {
                if (valid) {
                    blogTypesEdit(this.userForm).then(res => {
                        if (res.errCode == 0) {
                            this.$message.success('新增成功')
                            this.handleCancel()
                        } else {
                            this.$message.error(res.errMsg)
                        }
                    })
                } else {
                    this.$message.error('请填写完整信息')
                }
            })
        },
        handleCancel () {
            this.dialogVisible = false
            this.userForm = {
                _id: '',
                name: '',
                value: ''
            }
            this.$refs.userFormRef.resetFields()
            this.$emit('initTable')
        }
    }
}
</script>
